// 订单列表
var app =getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    all:[
      {
        value:"全部",
        checked:true
      },
      {
        value: "待付款",
        checked: false
      },
      {
        value: "待处理",
        checked: false
      },
      {
        value: "服务中",
        checked: false
      },
      {
        value: "已完成",
        checked: false
      },
    ],
    order:[],
    allorder: [],
    pagenum: 1,
    listnum: 10,
    //请求的状态
    status: 0,
    //是否正在请求
    req: false,
    //没有更多的了
    nomore: false,
    //是否显示详情
    show: false,
    //选择的订单
    chose:{}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.reqOrder(1, 0);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  },
  /**
   * 改变选中的状态
   */
  changeChecked: function(e) {
    console.log(e);
    var idx = e.currentTarget.dataset.index;
    var all = this.data.all;
    all.map(function(val) {
      val.checked = false;
    });
    all[idx].checked = true;
    this.setData({
      status: idx,
      all: all,
      order:[],
      req:false,
      nomore: false
    })
    this.reqOrder(1,idx);
  },
  /**
   * 查看停车说明
   */
  stopExplain: function(){
    wx.navigateTo({
      url: '/use/explain/explain',
    })
  },
  /**
   * 启动或停止设备
   */
  equipment: function(e) {
    var idx = e.currentTarget.dataset.index;
    var order = this.data.order;
    var that = this;
    // if (order[idx].device_status == 2 && order[idx].status==2){
    //   wx.showToast({
    //     title: "设备服务中，请耐心等待！",
    //     icon: "none"
    //   })
    //   return;
    // }
    var data = {
      id: order[idx].id,
      device_id: order[idx].device_id,
      product_iot_id: order[idx].product_iot_id,
      ordernumber: order[idx].ordernumber
    };
    app.getRequest('api/order/detailsUserOrder', {
      ordernumber: order[idx].ordernumber
    },function (e) {
      if (e.code === 200) {
        if (e.data.status === 4 || e.data.status === 34)
        rwx.showToast({
          title: '该订单已完成或服务中，无法操作！',
        })
        that.reqOrder(1, that.data.status);
      }
    })
    app.getRequest("api/device/qrcodeDevice", { device_qrcode_id: order[idx].device_qrcode_id }, function (e) {
      console.log(e)
      if (e.code === 200) {
        if (e.data.device_status == 2 && order[idx].status == 2){
          wx.showToast({
            title: '设备已启动，请耐心等待！',
          })
          return;
        }
        var redirectUrl = "";
        if (e.data.has_rolling == 1 && e.data.device_status == 1) {
          redirectUrl = '/use/door/door?qrcode=' + order[idx].device_qrcode_id + '&ids=' + JSON.stringify(data) + '&last_use_time=' + order[idx].last_use_time;
        } else {
          redirectUrl = '/use/work/work?qrcode=' + order[idx].device_qrcode_id + '&ids=' + JSON.stringify(data) + '&last_use_time=' + order[idx].last_use_time;
        }
        wx.navigateTo({
          url: redirectUrl,
        })
      }else{
        wx.showToast({
          title: e.message,
          icon: "none"
        })
      }
    });
    
  },
  /**
   * 请求列表
   */
  reqOrder: function(pagenum,status){
    var that = this;
    var req = this.data.req;
    var u_id = wx.getStorageSync("user").u_id;
    var listnum = this.data.listnum;
    var order = this.data.order;
    if(req == false) {
      that.setData({
        req: true
      })
        var params = {
          u_id: u_id,
          pagenum: pagenum,
          status: status,
          listnum: listnum
        }
        app.getRequest("api/order/selectUserOrder",params,function(e){
          if(e.code === 200) {
            var allorder = e.data.list;
            if(allorder.length > 0) {
              allorder.map(function(val){
                // val.ctime = app.time(val.ctime);
                order.push(val);
              })
              that.setData({
                order: order,
                req: false
              })
            } else {
              that.setData({
                nomore: true
              })
            }
          } else {
            wx.showToast({
              title: e.message,
              icon:"none"
            })
            that.setData({
              req: false
            })
          }
        })
    }
  },
  /**
   * 滚动分页
   */
  scrollBottom: function(){
    var pagenum = this.data.pagenum;
    pagenum = +pagenum + 1;
    this.setData({
      pagenum: pagenum
    })
    var status = this.data.status;
    this.reqOrder(pagenum,status);
  },
  /**
   * 查看订单详细
   */
  show: function(e){
    console.log(e)
    var order = this.data.order;
    var idx = e.currentTarget.dataset.index;
    var chose = order[idx];
    this.setData({
      show: true,
      chose: chose
    })
  },
  close:function(){
    this.setData({
      show: false
    })
  },
  // 立即支付
  payNow: function(e) {
    var order = this.data.order;
    var index = e.currentTarget.dataset.index;
    var chose = order[index];
    var u_id = chose.u_id;
    var d_id = chose.d_id;
    var c_m_id = chose.c_m_id;
    var y_id = chose.y_id;
    var pay_type = chose.pay_type;
    var ordernumber = chose.ordernumber;
    var params = {
      // u_id: u_id,
      // d_id: d_id,
      // c_m_id: c_m_id,
      // y_id: y_id,
      // pay_type: pay_type
      order_id: chose.id
    }
    app.getStatusByMsg(chose.device_id, chose.product_iot_id,function(getStatus){
    if (getStatus== 1) {
    app.getRequest("api/order/getByOrderNumberPay", params, function (e) {
      if (e.code == 200) {
        if (pay_type === 1) {
          var res = JSON.parse(e.data);
          var data = {
            device_id: chose.device_id,
            product_iot_id: chose.product_iot_id,
            ordernumber: ordernumber,
            id: chose.id
          }
          wx.requestPayment({
            timeStamp: res.timeStamp,
            nonceStr: res.nonceStr,
            package: res.package,
            signType: res.signType,
            paySign: res.paySign,
            success(res) {
              wx.showToast({
                title: "支付成功",
              })
              wx.redirectTo({
                url: '/use/door/door?qrcode=' + chose.device_qrcode_id + '&ids=' + JSON.stringify(data) + '&last_use_time=' + chose.last_use_time,
              })
            },
            fail(res) {
              wx.showModal({
                title: '',
                content: '支付失败',
                showCancel: false
              })
            }
          })
        } else {
          var data = {
            device_id: chose.device_id,
            product_iot_id: chose.product_iot_id,
            ordernumber: ordernumber
          }
          wx.redirectTo({
            url: '/use/door/door?ids=' + JSON.stringify(data) + '&last_use_time=' + chose.last_use_time,
          })
        }
      } else {
        wx.showModal({
          title: '',
          content: e.message,
          showCancel: false
        })
      }
      })
      }
    })
  }
})